package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.Admin;
import cn.moukou.sbzz.pojo.Bank;
import cn.moukou.sbzz.pojo.Enterprise;
import cn.moukou.sbzz.service.AdminService;
import cn.moukou.sbzz.service.BankService;
import cn.moukou.sbzz.service.EnterpriseService;
import cn.moukou.sbzz.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sbzz/api")
public class AuthController {

    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    BankService bankService;
    @Autowired
    AdminService adminService;

    @PostMapping("/login/enterprise")
    public Resp<Enterprise> enterpriseLogin(@RequestParam("username") String username, @RequestParam("password") String pwd) throws Exception {
        Enterprise enterprise = enterpriseService.enterpriseLogin(username, pwd);
        if(enterprise != null) {
            return Resp.ok(enterprise);
        } else {
            return Resp.fail("账号密码错误");
        }
    }

    @PostMapping("/login/bank")
    public Resp<Bank> bankLogin(@RequestParam("username") String username, @RequestParam("password") String pwd) throws Exception {
        Bank bank = bankService.bankLogin(username, pwd);
        if(bank != null) {
            return Resp.ok(bank);
        } else {
            return Resp.fail("账号密码错误");
        }
    }

    @PostMapping("/login/admin")
    public Resp<Admin> adminLogin(@RequestParam("username") String username, @RequestParam("password") String pwd) throws Exception {
        Admin admin = adminService.adminLogin(username, pwd);
        if(admin != null) {
            return Resp.ok(admin);
        } else {
            return Resp.fail("账号密码错误");
        }
    }

}
